﻿

namespace Microsoft.Redis
{
    /// <summary>
    /// 缓存模块(Redis)基类
    /// </summary>
    [Description("缓存模块(Redis)")]
    public abstract class RedisPackBase : PackBase
    {
        #region "属性"
        #region "获取 模块级别"
        /// <summary>
        /// 获取 模块级别，级别越小越先启动
        /// </summary>
        public override PackLevel Level => PackLevel.Framework;
        #endregion

        #endregion

        #region "方法"
        #region "将模块服务添加到依赖注入服务容器中"
        /// <summary>
        /// 将模块服务添加到依赖注入服务容器中
        /// </summary>
        /// <param name="services">依赖注入服务容器</param>
        /// <returns></returns>
        public override IServiceCollection AddServices(IServiceCollection services)
        {
            IConfiguration configuration = services.GetConfiguration();
            var enabled = configuration["EaCloud:Redis:Enabled"].CastTo(false);
            if (enabled)
            {
                string config = configuration["EaCloud:Redis:Configuration"];
                if (config.IsNullOrEmpty())
                {
                    throw new Exception(I18N.T("配置文件中 Redis 节点的 Configuration 不能为空"));
                }

                string name = configuration["EaCloud:Redis:InstanceName"].CastTo("RedisName");

                services.RemoveAll(typeof(IDistributedCache));
                services.AddStackExchangeRedisCache(opts =>
                {
                    opts.Configuration = config;
                    opts.InstanceName = name;
                });
            }
            return services;
        }
        #endregion

        #endregion

    }

}
